package com.buyhouse.zhaimao.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.buyhouse.android.grid.StaggeredGridView;
import com.buyhouse.zhaimao.BaseFragment;
import com.buyhouse.zhaimao.R;
import com.buyhouse.zhaimao.adapter.CommunityFragmentAdapter;
import com.buyhouse.zhaimao.adapter.CommunityShaiAdapter;
import com.buyhouse.zhaimao.model.CommunityBean;



/**
 * 找社区
 * 
 * @author
 *
 */
public class CommunityFragment extends BaseFragment implements  OnItemClickListener{
	/** 全部户型 **/
	private ListView mLv_huxing;
	/** 全部价格 **/
	private ListView mLv_price;
	/** 隐藏或者显示 筛选条件的 **/
	private LinearLayout mListview_show;
	/** 全部户型 **/
	private LinearLayout mLl_huxing;
	/** 全部价格 **/
	private LinearLayout mLl_price;
	/** 记录选中 **/
	private int pos_price = 0, pos_huxing = 0;
	/**  --价格--户型 **/
	private String price = "", huxing = "";
	/** 价格集合 (请求参数) **/
	private List<String> mPrice2;
	/** 户型集合 **/
	private List<String> mHuxing;
	/** 条件筛选的listview是否显示 默认不显示 **/
	private boolean isShow = false;
	/** 箭头显示 **/
	private ImageView mImg_price,mImg_huxing;
	private int position = 0;
	/** 适配器 **/
	private CommunityShaiAdapter mHuxingAdapter, mpriceAdapter;
	
	private StaggeredGridView sgv;
	/** 当前页号 */
	private int number = 1;
	/** 总记录数 */
	private long totalElements = 0;
	/** 总页数 */
	private int totalPages = 1;
	/** 是否有下一页 */
	private boolean hasMore = false;
	private CommunityFragmentAdapter adapter;
	private List<CommunityBean> list=new ArrayList<CommunityBean>();

	@Override
	protected int getContentView() {
		return R.layout.fragment_community;

	}

	@Override
	protected void initFragment() {
		
		initUi();
		initData();
	}

	private void initUi() {
		
//		View header = LayoutInflater.from(this.getActivity()).inflate(
//				R.layout.view_header, null);
		sgv = (StaggeredGridView) findViewById(R.id.grid_view);

//		sgv.addHeaderView(header);
		adapter= new CommunityFragmentAdapter(getActivity(), list);
		sgv.setAdapter(adapter);
		mLl_price = (LinearLayout)rootView. findViewById(R.id.all_price);
		mLl_huxing = (LinearLayout)rootView.findViewById(R.id.all_huxing);
		mLv_huxing= (ListView) rootView.findViewById(R.id.listview_huxing);
		mLv_price = (ListView) rootView.findViewById(R.id.listview_price);
		mImg_price=(ImageView)rootView.findViewById(R.id.img_prive);
		mImg_huxing=(ImageView)rootView.findViewById(R.id.img_huxing);
		mListview_show = (LinearLayout)rootView.findViewById(R.id.ll_show);
		
	}
	
	public void initData() {
//		mFinalHttp = new MFinalHttp<String>();

		

//		mSex = new ArrayList<String>();
//		mSex.add(getResources().getString(R.string.all));
//		mSex.add(getResources().getString(R.string.see_man));
//		mSex.add(getResources().getString(R.string.see_woman));
		mPrice2 = new ArrayList<String>();
		mPrice2.add("100");
		mPrice2.add("100-300");
		mPrice2.add("300-1000");
		mPrice2.add("1000以上");

		mHuxing = new ArrayList<String>();
		mHuxing.add("一        室");
		mHuxing.add("两        室");
		mHuxing.add("三        室");
		mHuxing.add("三室以上");

//		mCoaAdapter = new CoachAdapter(mCoash, getActivity());
//		mListview_coach.setAdapter(mCoaAdapter);
//
//		setListener();
		
		CommunityBean bean1=new CommunityBean();
		CommunityBean bean2=new CommunityBean();
		CommunityBean bean3=new CommunityBean();
		CommunityBean bean4=new CommunityBean();
		CommunityBean bean5=new CommunityBean();
		CommunityBean bean6=new CommunityBean();
		CommunityBean bean7=new CommunityBean();
		CommunityBean bean8=new CommunityBean();
		CommunityBean bean9=new CommunityBean();
		CommunityBean bean10=new CommunityBean();
		CommunityBean bean11=new CommunityBean();
		CommunityBean bean12=new CommunityBean();
		CommunityBean bean13=new CommunityBean();
		CommunityBean bean14=new CommunityBean();
		bean1.setName("北苑公寓");
		bean1.setHouseCount("12");
		bean1.setExpertCount("100");
		bean2.setName("第三方公寓");
		bean2.setHouseCount("4563");
		bean3.setExpertCount("55550");
		bean3.setExpertCount("50");
		bean4.setExpertCount("5");
		bean4.setExpertCount("50");
		list.add(bean1);
		list.add(bean2);
		list.add(bean3);
		list.add(bean4);
		list.add(bean5);
		list.add(bean6);
		list.add(bean7);
		list.add(bean8);
		list.add(bean9);
		list.add(bean10);
		list.add(bean11);
		list.add(bean12);
		list.add(bean13);
		list.add(bean14);

		mLv_price.setOnItemClickListener(this);
		mLv_huxing.setOnItemClickListener(this);
		mLl_price.setOnClickListener(this);
		mLl_huxing.setOnClickListener(this);
	}

	
	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		
		case R.id.all_price:// 售价

//			if (isShow && position == 1) {// listview显示时
//				isShow = false;
//				mListview_show.setVisibility(View.GONE);
//				mImg_price.setImageResource(R.drawable.black_bottom);
//			} else {// listview隐藏时 直接显示
				isShow = true;
				position = 1;
//				mLv_price.setVisibility(View.GONE);
				mLv_huxing.setVisibility(View.GONE);
//				mImg_screen.setImageResource(R.drawable.black_bottom);
//				mImg_direction.setImageResource(R.drawable.black_bottom);

				mpriceAdapter = new CommunityShaiAdapter(mPrice2, getActivity(), pos_price);
				mListview_show.setVisibility(View.VISIBLE);
				mLv_price.setVisibility(View.VISIBLE);
//				mImg_sex.setImageResource(R.drawable.black_top);
//			}
			mLv_price.setAdapter(mpriceAdapter);

			break;
		case R.id.all_huxing:// 户型

//			if (isShow && position == 2) {// listview显示时
//				isShow = false;
//				mListview_show.setVisibility(View.GONE);
//				mImg_huxing.setImageResource(R.drawable.black_bottom);
//			} else {// listview隐藏时 直接显示
				isShow = true;
				position = 2;
				mLv_price.setVisibility(View.GONE);
//				mImg_sex.setImageResource(R.drawable.black_bottom);
//				mImg_direction.setImageResource(R.drawable.black_bottom);

				mHuxingAdapter = new CommunityShaiAdapter(mHuxing,
						getActivity(), pos_huxing);
				mListview_show.setVisibility(View.VISIBLE);
				mLv_huxing.setVisibility(View.VISIBLE);
//				mImg_screen.setImageResource(R.drawable.black_top);

//			}
			mLv_huxing.setAdapter(mHuxingAdapter);

			break;

		default:
			break;
		}

	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		switch (parent.getId()) {
		
		case R.id.listview_price: // 全部价格

			pos_price = position;
			price = mPrice2.get(position);

			break;
		case R.id.listview_huxing: // 户型

			pos_huxing = position;
			huxing = position + "";
			break;
		default:
			break;
		}

		isShow = false;
		mListview_show.setVisibility(View.GONE);
//		page = 1;
//		getCoach(train, sex, city, orderStr, page + "");
		
	}
}
